/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.DateUtils;
import com.zx.pm.common.utils.excel.ExportExcel;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.Total;
import com.zx.pm.modules.zx.service.TotalService;

/**
 * 项目信息统计Controller
 * @author forest
 * @version 2019-01-30
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/total")
public class TotalController extends BaseController{

	@Autowired
	private TotalService totalService;
	
	@ModelAttribute
	public Total get() {
		return new Total();
	}
	
	@RequiresPermissions("zx:total:view")
	@RequestMapping(value = {"list", ""})
	public String list(Total total, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Total> page = totalService.findPage(new Page<Total>(request, response), total); 
		model.addAttribute("pageFalg", "totalList");
		model.addAttribute("page", page);
		model.addAttribute("total", total);
		return "modules/zx/totalList";
	}

	/**
	 * 导出项目信息数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("zx:total:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(Total total, HttpServletRequest request, HttpServletResponse response, 
    		RedirectAttributes redirectAttributes) {
		try {
            String fileName = "项目信息数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
    		new ExportExcel("项目信息", Total.class).setDataList(totalService.findList(total)).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出项目信息失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/zx/total/list?repage";
    }
}
